package com.cheyun.netsalev3.viewmodel

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.lifecycle.MutableLiveData
import com.cheyun.netsalev3.bean.AZItemEntity
import com.cheyun.netsalev3.bean.CarPinPaParam
import com.cheyun.netsalev3.repository.ChooseCityRepository
import com.cheyun.netsalev3.utils.LettersComparator
import com.cheyun.netsalev3.utils.PinyinUtils
import com.cheyun.netsalev3.view.choosecity.ChooseCityActivity
import java.io.Serializable
import java.util.*

class ChooseCityActivityViewModel(var chooseCityRepository: ChooseCityRepository) : BaseViewModel() {
    var dataList = MutableLiveData<List<AZItemEntity<CarPinPaParam>>>()

    //市
    var citylist = MutableLiveData<List<CarPinPaParam>>()
    //区
    var qulist = MutableLiveData<List<CarPinPaParam>>()


    init {
        chooseCityRepository.getDistrict(1, 0, this::setData)
    }

    fun setData(list: ArrayList<CarPinPaParam>) {
//        var loginParam = MySharedPreferences.getLoginData()
//        loginParam?.channel?.apply {
//            var carPinPaParam = CarPinPaParam(this.brand, "★", this.brandname)
//            carPinPaParam.IsTop = true
//            list.add(carPinPaParam)
//        }
        val dateList = fillData(list)
        Collections.sort(dateList, LettersComparator())
        dataList.postValue(dateList)
    }


    private fun fillData(date: List<CarPinPaParam>): List<AZItemEntity<CarPinPaParam>> {
        val sortList = ArrayList<AZItemEntity<CarPinPaParam>>()
        for (i in 0 until date.size) {
            var aDate = date[i]
            val item = AZItemEntity<CarPinPaParam>(null, null)
            item.value = aDate
            if (aDate.letter == "★") {
                item.sortLetters = "★"
            } else {
                //汉字转换成拼音
                val pinyin = PinyinUtils.getPingYin(aDate.title)
                //取第一个首字母
                val letters = pinyin.substring(0, 1).toUpperCase()
                // 正则表达式，判断首字母是否是英文字母
                if (letters.matches("[A-Z]".toRegex())) {
                    item.sortLetters = letters.toUpperCase()
                } else {
                    item.sortLetters = "★"
                }
            }
            sortList.add(item)

        }
        return sortList

    }

    /**
     * 点击省
     */
    fun onClickShen(view: View, item: AZItemEntity<CarPinPaParam>) {
        shen = item.value!!
        item.value?.id?.apply {
            chooseCityRepository.getDistrict(2, this, this@ChooseCityActivityViewModel::setShen)
        }
        var actvity = view.context as ChooseCityActivity
        actvity.binding.viewPager.currentItem = 1


    }

    //选中的item 省市 区
    var shen = CarPinPaParam(0, "", "")
    var shi = CarPinPaParam(0, "", "")
    var que = CarPinPaParam(0, "", "")
    /**
     * 省 回调
     */
    fun setShen(list: ArrayList<CarPinPaParam>) {
        citylist.postValue(list)
    }

    lateinit var mActivity:Activity
    fun onClickItem(view: View, item: CarPinPaParam) {
        var actvity = view.context as ChooseCityActivity
        var currentItem = actvity.binding.viewPager.currentItem
        mActivity = actvity
        //点击城市
        if (currentItem == 1) {
            shi = item
            item?.id?.apply {
                chooseCityRepository.getDistrict(3, this, this@ChooseCityActivityViewModel::setCity)
            }
            actvity.binding.viewPager.currentItem = 2
        } else {
            que = item
            //点击区
            var intent = Intent()
            intent.putExtra("shen", shen as Serializable)
            intent.putExtra("shi", shi as Serializable)
            intent.putExtra("que", que as Serializable)
            actvity.setResult(103, intent)
            actvity.finish()
        }
    }

    /**
     * 城市 回调
     */
    fun setCity(list: ArrayList<CarPinPaParam>) {
        qulist.postValue(list)
        if(list == null || list.size == 0) {
            if (mActivity != null) {
                var intent = Intent()
                intent.putExtra("shen", shen as Serializable)
                intent.putExtra("shi", shi as Serializable)
                intent.putExtra("que", que as Serializable)
                mActivity.setResult(103, intent)
                mActivity.finish()
            }
        }
    }
}